package bbs.net;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**上拉加载20条我的帖子
 * Created by nimi on 2015/11/24.
 */
public class GetMoreTieZi {
    MyPost.Download download;
    Delegate delegate;
    Upload upload;
    public GetMoreTieZi(Upload upload){
        this.upload=upload;
    }

    public Upload getUpload() {
        return upload;
    }

    public void setUpload(Upload upload) {
        this.upload = upload;
    }

    public MyPost.Download getDownload() {
        return download;
    }

    public void setDownload(MyPost.Download download) {
        this.download = download;
    }
    public Delegate getDelegate() {
        return delegate;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }


    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/bbs/GetMoreTieZi";
        RequestQueue requestQueue = VolleySingleton.getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest =new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            public void onResponse(String response) {
                afterGet(response);
                if(delegate!=null)
                    delegate.onResponse(response,GetMoreTieZi.this);
            }}, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error, GetMoreTieZi.this);

            }
        })

        {
            @Override
            protected Map<String, String> getParams () {
                Map map = new HashMap();
                map.put("data",JSON.toJSONString(upload));
                return map;
            }
            @Override
            protected Response<String> parseNetworkResponse (NetworkResponse response){
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }

        };
        requestQueue.add(stringRequest);
    }
    private void afterGet(String response) {
        download = JSON.parseObject(response, MyPost.Download.class);
    }
    // 上行数据
    public static class Upload
    {
        // 当前显示的最后一条帖子的时间
        String lastTime;
        // 用户id
        String userId;
        public String getLastTime() {
            return lastTime;
        }
        public void setLastTime(String time) {
            this.lastTime = time;
        }
        public String getUserId() {
            return userId;
        }
        public void setUserId(String userId1) {
            this.userId = userId1;
        }


    }
    public static interface Delegate
    {
        public void onResponse(String response, GetMoreTieZi getMoreTieZi);
        public void onErrorResponse(VolleyError error, GetMoreTieZi getMoreTieZi);
    }

}
